<script setup lang="ts">
import type { server } from 'typescript';
// import { helloUsingPost } from "../api/ceshi1"
import { loginUsingPost } from '../api/ceshi2';
import { downloadLocalUsingPost } from '@/api/downloadFile';
import type { API } from '../api/typings';
import { ref } from 'vue';
import { ElMessage } from 'element-plus'
// import { saveAs } from 'file-saver';

const id = ref('')
const psd = ref('')

const hello = async () => {
    console.log(id)
    const params: API.loginUsingPOSTParams = {
        // 按定义准确填写参数
        id: id.value,
        psd: psd.value,
    };
    try {
        const response = await loginUsingPost(params);
        // alert('请求成功');
        console.log('请求成功，响应内容：', response);

        // await downloadLocalUsingGet();

    } catch (error) {
        // alert('请求失败');
        console.log('请求失败，错误信息：', error);
    }
}

const fileName = ref('')
const downloadFile = async () => {
    const params: API.downloadLocalUsingPOSTParams = {
        // 按定义准确填写参数
        fileName: fileName.value
    }
    try {
        const response = await downloadLocalUsingPost(params) as any;
        console.log(response)
        // console.log(response.data)
        // console.log(response.request)
        const contentType = response.headers['content-type'];
        const blob = new Blob([response.data], { type: contentType });
        // saveAs(blob, fileName.value)
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement('a');
        link.href = url;
        link.download = params.fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
    } catch (error) {
        ElMessage.error('文件下载出错,请检查文件名是否正确')
        // console.error('文件下载出错:', error);
    }
}

// const downloadImg = async () => {
//     const params: API.downloadLocalUsingPOSTParams = {
//         // 按定义准确填写参数
//         fileName: "a1.jpg"
//     }
//     try {
//         const response = await downloadLocalUsingPost(params);
//         console.log(response)
//         const blob = new Blob([response.data], { type: 'application/octet-stream' });
//         const url = URL.createObjectURL(blob);
//         const link = document.createElement('a');
//         link.href = url;
//         link.download = "a1.jpg";
//         document.body.appendChild(link);
//         link.click();
//         document.body.removeChild(link);
//         URL.revokeObjectURL(url);
//     } catch (error) {
//         console.error('文件下载出错:', error);
//     }
// }

</script>

<template>
    <div>
        <form>
            <input type="text" v-model="id" placeholder="请输入账号">
            </br>
            <input type="password" v-model="psd" placeholder="请输入密码">
            </br>
        </form>
    </div>

    <button v-on:click=hello>
        点击访问
    </button>

    <div>
        <input v-model="fileName" type="text" placeholder="请输入文件名">
        <button v-on:click="downloadFile">点击下载文件 </button>
        <!-- <button v-on:click="downloadImg">点击下载jpg </button> -->
    </div>
</template>
